package day1109.observer;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

import static common.PrintUtil.printLine;

/**
 * @author Mr.ma
 * @since 2022/11/9 16:55
 **/
public class AuctioneerProxy implements MethodInterceptor {
    private Object target;
    public AuctioneerProxy(Object target) {
        this.target = target;
    }
    public Object getInstance(){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        Object invoke = null;
        listen(objects);
        invoke = method.invoke(target, objects);
        notice(objects);
        return invoke;
    }
    public void listen(Object[] objects){
        for (Object object : objects) {
            System.out.println("当前价格为：" + object);
        }
    }
    public void notice(Object[] objects){
        for (Object object : objects) {
            System.out.println(object + "元，第一次");
        }
    }
}
